package com.xinnet.core.validate;

import java.util.Properties;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.xinnet.core.validate.attribute.AlphaNum2;


/**
 * 扩展半角英数字(alphanum2)用的Validator类。
 * 
 * @author wenjianye 请输入半角英数字或横线(-)、下滑线(_)、句号(.)、加号(+)。
 */
public class AlphaNum2Validator  extends Validator  implements
		ConstraintValidator<AlphaNum2, Object> {

	private static final String REGEX_PATTERN_ALPHANUM2 = "[-A-Za-z0-9_\\.\\+]*";
	private static Properties prop;
	@Override
	public void initialize(AlphaNum2 arg0) {

	}

	@Override
	public boolean isValid(Object value,
			ConstraintValidatorContext constraintContext) {
		return super.isValid(value,constraintContext);
	}

	/**
	 * 验证属性的方法 未通过验证:返回false; 通过验证: 返回 true;
	 * */
	public boolean valid(Object value) {
		if (value == null || value instanceof Number) {
			return false;
		}
		return ValidatorUtil.patternMatches(REGEX_PATTERN_ALPHANUM2, value);
	}

	public String getMessage() {
		return prop
				.getProperty(this.getClass().getName());
	}

}
